package org.bright.config;

import com.github.pagehelper.PageInterceptor;
import org.apache.ibatis.plugin.Interceptor;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.context.annotation.Bean;

import javax.sql.DataSource;
import java.util.Properties;

public class MybatisConfig {
    @Bean
    public SqlSessionFactoryBean sqlSessionFactoryBean(DataSource dataSource){
// 创建SqlSessionFactoryBean实例，用于配置MyBatis工厂
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
// 设置数据源，使MyBatis能连接到数据库
        sqlSessionFactoryBean.setDataSource(dataSource);

// 初始化PageInterceptor实例，用于MyBatis的分页插件
        PageInterceptor pageInterceptor = new PageInterceptor();
// 创建Properties实例，用于配置分页插件的属性
        Properties properties = new Properties();
// 设置分页插件的属性，启用合理化分页，默认为false
        properties.setProperty("reasonable","true");
// 设置分页插件的属性，指定数据库方言，这里使用MySQL
        properties.setProperty("helperDialect","mysql");
// 将属性应用到分页插件实例
        pageInterceptor.setProperties(properties);
        sqlSessionFactoryBean.setPlugins(new Interceptor[]{pageInterceptor});
        return sqlSessionFactoryBean;
    }
    @Bean
    public MapperScannerConfigurer mapperScannerConfigurer(){
        MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
        mapperScannerConfigurer.setBasePackage("org.bright.mapper");
        return mapperScannerConfigurer;
    }
//    配置分页插件
}
